package com.sgu6.sgu6_machine.container;

import android.util.Log;

import com.sgu6.middleware.enums.MachineBitEnum;
import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.event.WarningEvent;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.netty.container.Machine;
import com.sgu6.netty.utils.NettyHelper;
import com.sgu6.sgu6_machine.api.IWarnApi;
import com.sgu6.sgu6_machine.api.WarnServiceImpl;
import com.sgu6.sgu6_machine.common.CmdTranslator;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.db.DBManager;
import com.sgu6.sgu6_machine.event.MillStatusEvent;
import com.sgu6.sgu6_machine.event.PowerSaveEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

public class ComContainer {
    protected static final String TAG = ComContainer.class.getSimpleName();
    public static CmdTranslator ct = new CmdTranslator();
    public static boolean isRecordHeart = false;
    private static IWarnApi api = new WarnServiceImpl();
    //碾米超时记录碾米重量
    private long weight = 0L;


    public static void setCmd(String cmd) throws Exception {

        String bitCmd = cmd.substring(2, 6);

        if (bitCmd.equals("000A")){
            EventBus.getDefault().post(new PowerSaveEvent(1, HexUtil.HexString2Bytes(cmd)));
        }
        if (bitCmd.equals("000B")){
            EventBus.getDefault().post(new PowerSaveEvent(2,HexUtil.HexString2Bytes(cmd)));
        }
        if (bitCmd.equals("000C")){
            EventBus.getDefault().post(new PowerSaveEvent(3,HexUtil.HexString2Bytes(cmd)));
        }
        if (bitCmd.equals("000D")){
            EventBus.getDefault().post(new PowerSaveEvent(4,HexUtil.HexString2Bytes(cmd)));
        }


        //检测心跳信息
        if (bitCmd.equals(MachineBitEnum.心跳包.code())) {
            HashMap<String, Long> machineStatus = ct.translateTest2Map(cmd);

            if (machineStatus.get("hTemp") >= MachineContainer.machineConfigDto.millTemp * 10 && machineStatus.get("vTemp") >= MachineContainer.machineConfigDto.millTemp * 10) {
                MachineContainer.machineTempOK = true;
            } else {
                MachineContainer.machineTempOK = false;
            }

            Machine.heartInfo = "横温:" + String.format("%.1f", machineStatus.get("hTemp") * 0.1)
                    + "℃，纵温:" + String.format("%.1f", machineStatus.get("vTemp") * 0.1)
                    + "℃，重量:" + machineStatus.get("weight")
                    + "g，状态:" + ct.analyMainStatusValue(machineStatus);
        }

        //检测报警信息
        if (bitCmd.equals(MachineBitEnum.警报.code()) && MachineContainer.isPay) {

            int value = Integer.parseInt(cmd.substring(10, 14), 16);
            String warnValue = ct.get32BitBinString(value);

            if (warnValue.charAt(31) == '1') {
                api.overTemp(cmd);
            } else if (warnValue.charAt(30) == '1') {
                api.heatTimeout(cmd);
            } else if (warnValue.charAt(29) == '1') {
                api.millTimeout(cmd);
            } else if (warnValue.charAt(28) == '1') {
                api.dropTimeout(cmd);
            } else if (warnValue.charAt(27) == '1') {
                api.filmTimeout(cmd);
            } else if (warnValue.charAt(26) == '1') {
                api.sealTimeout(cmd);
            } else if (warnValue.charAt(25) == '1') {
                api.openDoorTimeout(cmd);
            } else if (warnValue.charAt(24) == '1') {
                api.closeDoorTimeout(cmd);
            } else if (warnValue.charAt(23) == '1') {
                api.pickupTimeout(cmd);
            } else if (warnValue.charAt(22) == '1') {
                api.sendValleyTimeout(cmd);
            } else if (warnValue.charAt(21) == '1') {
                api.filmFeedTimeout(cmd);
            } else if (warnValue.charAt(20) == '1') {
                api.accuracyMaxStep(cmd);
            } else if (warnValue.charAt(19) == '1') {
                api.branFullTimeout(cmd);
            }

        }
        //检测碾米状态
        if (bitCmd.equals(MachineBitEnum.当前工作状态.code()) | bitCmd.equals("0044")) {
            Log.i(TAG, "setCmd: " + bitCmd);
            EventBus.getDefault().post(new MillStatusEvent(cmd));
            //推送碾米状态到服务器日志
            NettyHelper.postMsgWithCommandResp(MachineContainer.machineConfigDto.machineNo, cmd);
        }

        //检测开关门状态
        if (bitCmd.equals(MachineBitEnum.灯箱动作.code()) ) {
            //推送碾米状态到服务器日志
            NettyHelper.postMsgWithCommandResp(MachineContainer.machineConfigDto.machineNo, cmd);
        }

        //获取称重斗重量
        if (bitCmd.equals(MachineBitEnum.称重重量.code())) {
            if (!MachineContainer.machineConfigDto.withPack) {
                api.millWeight(cmd);
            }
        }

    }

}
